import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../services/address.dart';
import '../../services/dio_manager.dart';
import '../../utils/persisten_storage.dart';
import '../bottom_tab_controller.dart';
import 'login_model.dart';

class LoginController extends GetxController with GetSingleTickerProviderStateMixin  {

  late TabController tabController;

  var usernameTF = TextEditingController();
  var passwordTF = TextEditingController();

  bool displayPsw = true;
  bool isRememberPs = true;
  /// 登录请求

//登陆类型：0：用帐号与密码登陆  1：手机验证码登陆  2，发送手机验证码，
// 3：微信扫码登陆  4，APP验证码登陆
  requestDataWithLogin(var login,var password)async{

    var params = {
      'login':login,
      'password':password,
      'type':'0',
      'registration_id':'${await PersistentStorage().getStorage('registration_id')}'
    };

    var json = await DioManager().kkRequest(Address.login,bodyParams: params,isShowLoad: true);

    Usesr model = Usesr.fromJson(json);

    if (model.errmsg == 'ok') {
      // await prefs.setString('acctoken', _model.data.accessToken);
      PersistentStorage().setStorage('acctoken', model.data!.accessToken);
      PersistentStorage().setStorage('uid', model.data!.uid);
      PersistentStorage().setStorage('partner_id', model.data!.partnerId);
      PersistentStorage().setStorage('image128', model.data!.image128);
      PersistentStorage().setStorage('job_company_id', model.data!.jobCompanyId.toString());
      PersistentStorage().setStorage('job_user_id', model.data!.jobUserId.toString());
      PersistentStorage().setStorage('name', model.data!.name);
      PersistentStorage().setStorage('login_mobile', model.data!.login_mobile);
      PersistentStorage().setStorage('companyIdName', model.data!.companyIdName);
      PersistentStorage().setStorage('companyId', model.data!.companyId.toString());
      PersistentStorage().setStorage('company_ids_name', model.data!.companyIdsName);
      PersistentStorage().setStorage('dtcloud_login_url', model.data!.dtcloud_login_url);
      PersistentStorage().setStorage('icon', model.data!.icon);

      Get.offAll(Tabs());

      // Get.to(HomePage());

    } else {
      BotToast.showText(text: json['message']);
    }

  }

  /// 显示或隐藏密码

  tapClickPasswordShow(){
    displayPsw =! displayPsw;
    update();
  }

  /// 点击登录
  tapClickLoginBtn()async{
    if(usernameTF.text.isEmpty||passwordTF.text.isEmpty){
      BotToast.showText(text: '请输入用户名密码');
      return;
    }

    requestDataWithLogin(usernameTF.text,passwordTF.text);
    if(isRememberPs == true){
      getUsernameAndPassWord();
    }
    FocusScope.of(Get.context!)
        .requestFocus(FocusNode());
  }

  tapClickRememberPassWord()async{
    isRememberPs=!isRememberPs;
    if(isRememberPs == true){
      getUsernameAndPassWord();
    }else{
      clearUserNameAndPassWord();
    }
    update();
  }
  /// 初始化记住密码
  void initUrl() async {
    var prefs = await SharedPreferences.getInstance();
    print('11111 ===${prefs.getString('username')}');
    // print(prefs.getString('check'));

    usernameTF.text = prefs.getString('username')!;
    passwordTF.text = prefs.getString('pwd')!;
    if (prefs.containsKey('check') == false) {
      isRememberPs = false;
    } else {
      isRememberPs = prefs.getBool('check')!;
    }
    update();
  }

  /// 记住密码
  void getUsernameAndPassWord() async {
    var prefs = await SharedPreferences.getInstance();
    prefs.setString('username', usernameTF.text);
    prefs.setString('pwd', passwordTF.text);
    prefs.setBool('check', true);
  }

  /// 清除密码
  void clearUserNameAndPassWord() async {
    var prefs = await SharedPreferences.getInstance();
    prefs.remove('username');
    prefs.remove('pwd');
    usernameTF.text = '';
    passwordTF.text = '';
    prefs.setBool('check', false);
    isRememberPs = false;
  }

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    initUrl();
    tabController = TabController(length: 2, vsync: this);



  }
}